package com.ys.service.sys.impl;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.bo.enume.sys.StatusEnum;
import com.ys.entity.sys.SysButton;
import com.ys.entity.sys.SysCol;
import com.ys.entity.sys.SysDict;
import com.ys.entity.sys.SysMenu;
import com.ys.mapper.business.CommonMapper;
import com.ys.mapper.sys.SysCommonMapper;
import com.ys.service.sys.CommonService;
import com.ys.vo.R;
import com.ys.vo.W;

import tk.mybatis.mapper.util.StringUtil;

@Service("commonService")
public class CommonServiceImpl implements CommonService {

//	@Autowired
	@Resource
	private CommonMapper commonMapper;
	@Resource
	private SysCommonMapper sysCommonMapper;
	
	@Override
	public String indexCommonOfMenu(Model model,
    		HttpServletRequest request,
    		Long menuId) {
		
		SysMenu sysMenu = new SysMenu().setId(menuId).queryFirst();
		if (sysMenu == null) {
			return W.error(model, "页面不存在");
		}
		
		String pathGridRequest = sysMenu.getPathGridRequest();
		String pathGridEditRequest = sysMenu.getPathGridEditRequest();
		if (pathGridEditRequest == null || pathGridEditRequest.length() == 0) {
			return W.error(model, "尚未设置表格编辑请求路径，请在数据库手动配置");
		}
		GridDto.MenuDto menu = new GridDto.MenuDto(pathGridRequest, pathGridEditRequest);
		
		List<SysCol> listSysColTable = new SysCol()
				.setMenuId(menuId)
				.setOrderby("A.sort_no asc")
				.queryList();
		
		// ID-sys_col的MAP
		Map<Long, SysCol> ID_SYS_COL_MAP = new HashMap<Long, SysCol>();
		List<SysCol> listSysCol = new SysCol().setMenuId(menuId).queryList();
		for(SysCol sysCol : listSysCol) {
			ID_SYS_COL_MAP.put(sysCol.getId(), sysCol);
		}
		
		// dictKey-List<SysDict>的MAP
		Map<String, List<SysDict>> KEY_LISTDICT_MAP = new HashMap<String, List<SysDict>>();
		List<SysDict> listSysDict = new SysDict().setStatus(StatusEnum.ON_USED.value).queryList();
		for(SysDict sysDict : listSysDict) {
			String dictKey = sysDict.getDictKey();
			if (KEY_LISTDICT_MAP.containsKey(dictKey)) {
				KEY_LISTDICT_MAP.get(dictKey).add(sysDict);
			} else {
				List<SysDict> listValue = new ArrayList<SysDict>();
				listValue.add(sysDict);
				KEY_LISTDICT_MAP.put(dictKey, listValue);
			}
		}
		List<String> colGridArr = null;
		try {
//			colGridArr = GridUtil.listColumn(listSysColTable, sysMenu, ID_SYS_COL_MAP, KEY_LISTDICT_MAP);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}

		// 工具栏
		GridDto.ToolbarDto toolbar = null;
		{
			List<SysButton> listSysButton = new SysButton().setMenuId(menuId).queryList();
			String buttonHtml = "";
			String buttonScript = "";
//			for(SysButton sysButton : listSysButton) {
//				buttonHtml = buttonHtml + sysButton.getEleHtml();
//				buttonScript = buttonScript + sysButton.getScript();
//			}
//			toolbar = new GridDto.ToolbarDto(buttonHtml, buttonScript);
		}
		GridDto dto = new GridDto(menu, colGridArr, toolbar);
		model.addAttribute("dto", dto);
		return sysMenu.getTempletPath();
	}

	@Override
	public R editOnTable(HttpServletRequest request) {
		
		// 字段-值的MAP
		Map<String, String> paramMap = new HashMap<String, String>();
		Enumeration<String> reqNames = request.getParameterNames();
		for (; reqNames.hasMoreElements();) {
			String aaa = reqNames.nextElement();
			System.out.println(aaa + "=====" + request.getParameter(aaa));
			
			paramMap.put(StringUtil.camelhumpToUnderline(aaa), request.getParameter(aaa));
		}

		// 单独处理：sys_menu_id
		String KEY_sysMenuId = "sys_menu_id";
		if (!paramMap.containsKey(KEY_sysMenuId) || paramMap.get(KEY_sysMenuId) == null || paramMap.get(KEY_sysMenuId).length() == 0) {
			return R.error("菜单ID不能为空");
		}
		Long sysMenuId = Long.parseLong(paramMap.get(KEY_sysMenuId));
		paramMap.remove(KEY_sysMenuId);
		SysMenu sysMenu = new SysMenu().setId(sysMenuId).queryFirst();
		String tableName = "`" + sysMenu.getDatabaseTable() + "`";

		// 单独处理键：id
		String KEY_id = "id";
		if (!paramMap.containsKey(KEY_id) || paramMap.get(KEY_id) == null || paramMap.get(KEY_id).length() == 0) {
			return R.error("主键ID不能为空");
		}
		Long id = Long.parseLong(paramMap.get(KEY_id));
		paramMap.remove(KEY_id);
		
		if (sysMenu.getDatabaseTable().startsWith("sys_")) {
			sysCommonMapper.updateByMap(paramMap, tableName, id);
		} else {
			commonMapper.updateByMap(paramMap, tableName, id);
		}
		
		return R.ok("修改成功");
	}
	@Override
	public R edit(Map<String, Object> params) {

//		commonMapper.updateByMap(params, null, null);
		return R.ok("操作成功");
	}
}
